/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FramePrincipal.java
 *
 * Created on 30-mar-2009, 17:31:42
 */

package View;

import Aleatorio.Distribuciones.GeneradorAleatorio;
import Aleatorio.Distribuciones.GeneradorExponencial;
import Aleatorio.Distribuciones.GeneradorNormal;
import Aleatorio.Distribuciones.GeneradorPoisson;
import Aleatorio.Distribuciones.GeneradorUniforme;
import Aleatorio.GeneradorIntervalos;
import Aleatorio.Intervalo;
import Utils.Graficador;
import javax.swing.JOptionPane;
import org.jfree.chart.ChartPanel;

/**
 *
 * @author Sobrenombre
 */
public class FramePrincipal extends javax.swing.JFrame {

    /** Creates new form FramePrincipal */
    public FramePrincipal() {
        initComponents();
    }

    public void desactivarControles(){
        txtDesviacion.setEditable(false);
        txtLambda.setEditable(false);
        txtMedia.setEditable(false);

        

        txtDesviacion.setText("");
        txtLambda.setText("");
        txtMedia.setText("");
    }

    public void activarExponencial(){
        desactivarControles();
        txtMedia.setEditable(true);
        txtMedia.setText("1");
    }

    public void activarNormal(){
        desactivarControles();
        txtMedia.setEditable(true);
        txtDesviacion.setEditable(true);
        txtMedia.setText("0");
        txtDesviacion.setText("1");
    }

    public void activarPoisson(){
        desactivarControles();
        txtLambda.setEditable(true);
        txtLambda.setText("1");
    }

    public Intervalo[] generarDistribucion(GeneradorAleatorio generador, int cantidad, int cantIntervalos){
        double[] nums = new double[cantidad];
        GeneradorIntervalos generadorIntervalos = new GeneradorIntervalos(cantIntervalos);

        for (int i = 0; i < nums.length; i++) {
            nums[i] = generador.siguiente();
        }

        return generadorIntervalos.generarDistribucion(nums);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        optExponencial = new javax.swing.JRadioButton();
        optNormal = new javax.swing.JRadioButton();
        optPoisson = new javax.swing.JRadioButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtSemilla = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtNumeros = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtIntervalos = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        txtDesviacion = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtMedia = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtLambda = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Generación de Distribuciones");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Distribución"));

        buttonGroup1.add(optExponencial);
        optExponencial.setText("Exponencial negativa");
        optExponencial.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                optExponencialActionPerformed(evt);
            }
        });

        buttonGroup1.add(optNormal);
        optNormal.setText("Normal (Box Muller)");
        optNormal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                optNormalActionPerformed(evt);
            }
        });

        buttonGroup1.add(optPoisson);
        optPoisson.setText("Poisson");
        optPoisson.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                optPoissonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(optExponencial)
                .addGap(18, 18, 18)
                .addComponent(optNormal)
                .addGap(18, 18, 18)
                .addComponent(optPoisson)
                .addContainerGap(49, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(optExponencial)
                    .addComponent(optNormal)
                    .addComponent(optPoisson))
                .addContainerGap(17, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Parámetros Comunes"));

        jLabel1.setText("Semilla:");

        txtSemilla.setText("10");

        jLabel5.setText("Números:");

        txtNumeros.setText("1000000");

        jLabel6.setText("Intervalos:");

        txtIntervalos.setText("10");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtSemilla, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtNumeros, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtIntervalos, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtSemilla, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(txtNumeros, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(txtIntervalos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButton1.setText("Generar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Salir");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Parámetros Específicos"));

        jLabel3.setText("Desviación:");

        txtDesviacion.setEditable(false);

        jLabel2.setText("Media:");

        txtMedia.setEditable(false);

        jLabel4.setText("Lambda:");

        txtLambda.setEditable(false);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtDesviacion, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(37, 37, 37)
                        .addComponent(txtMedia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(10, 10, 10)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtLambda, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(73, Short.MAX_VALUE))
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {txtDesviacion, txtMedia});

        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtMedia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtLambda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDesviacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void optExponencialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_optExponencialActionPerformed
        activarExponencial();
}//GEN-LAST:event_optExponencialActionPerformed

    private void optNormalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_optNormalActionPerformed
        activarNormal();
    }//GEN-LAST:event_optNormalActionPerformed

    private void optPoissonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_optPoissonActionPerformed
        activarPoisson();
    }//GEN-LAST:event_optPoissonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if(Validations.vacio(txtSemilla, "Debe ingresar una semilla")
           && Validations.numericoEntero(txtSemilla, "La semilla deber ser un número entero")
           && Validations.vacio(txtNumeros, "Debe ingresar cantidad de números a generar")
           && Validations.numericoEntero(txtNumeros, "La cantidad de números debe ser entero")
           && Validations.vacio(txtIntervalos, "Debe ingresar la cantidad de intervalos")
           && Validations.numericoEntero(txtIntervalos, "La cantidad de intervalos deber ser entero")
           && ((optExponencial.isSelected() && Validations.vacio(txtMedia, "Debe ingresar la media")
                && Validations.numericoDouble(txtMedia, "La media debe ser número decimal positivo", 1)
                && Validations.esCero(txtMedia, "La media no puede valer 0"))
             || (optNormal.isSelected() && Validations.vacio(txtMedia, "Debe ingresar la media")
                && Validations.numericoDouble(txtMedia, "La media debe ser un número decimal", 0)
                && Validations.vacio(txtDesviacion, "Debe ingresar la desviación")
                && Validations.numericoDouble(txtDesviacion, "La desviación debe ser un número decimal positivo", 1))
             || (optPoisson.isSelected() && Validations.vacio(txtLambda, "Debe ingresar lambda")
                && Validations.numericoDouble(txtLambda, "Lambda debe ser un número decimal positivo", 1)
                && Validations.esCero(txtLambda, "Lambda no puede ser 0")))){

                String title = "";
                GeneradorAleatorio generador = null;
                
                if (optExponencial.isSelected()){
                    generador = new GeneradorExponencial(Integer.parseInt(txtSemilla.getText()), Integer.parseInt(txtMedia.getText()));
                    title = "Distribución Exponencial Negativa con Media: "+txtMedia.getText();
                }
                if (optNormal.isSelected()){
                    generador = new GeneradorNormal(Integer.parseInt(txtSemilla.getText()),Double.parseDouble(txtMedia.getText()), Double.parseDouble(txtDesviacion.getText()));
                    title = "Distribución Normal con Media: "+txtMedia.getText()+" y Desviación: "+txtDesviacion.getText();
                }
                if (optPoisson.isSelected()){
                    generador = new GeneradorPoisson(Integer.parseInt(txtSemilla.getText()),Double.parseDouble(txtLambda.getText()));
                   // generador = new GeneradorPoisson(Integer.parseInt(txtSemilla.getText()));
                    title = "Distribución Poisson con Lambda: "+txtLambda.getText();
                }

                Intervalo[] intervalos = generarDistribucion(generador, Integer.parseInt(txtNumeros.getText()), Integer.parseInt(txtIntervalos.getText()));
                ChartPanel chartPanel = Graficador.graficarBarras(intervalos, title, "Intervalos", "Frecuencias");
                

                DialogGrafico dialog = new DialogGrafico(this, true, chartPanel);
                dialog.setLocationRelativeTo(this);
                dialog.setVisible(true);

        }

            
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jButton2ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JRadioButton optExponencial;
    private javax.swing.JRadioButton optNormal;
    private javax.swing.JRadioButton optPoisson;
    private javax.swing.JTextField txtDesviacion;
    private javax.swing.JTextField txtIntervalos;
    private javax.swing.JTextField txtLambda;
    private javax.swing.JTextField txtMedia;
    private javax.swing.JTextField txtNumeros;
    private javax.swing.JTextField txtSemilla;
    // End of variables declaration//GEN-END:variables

}
